package com.idea.dsl.validator;

import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * DSL验证器工厂
 */
@Component
public class DslValidatorFactory {

    private final List<DslValidator> validators;
    private final Map<String, DslValidator> validatorMap;

    public DslValidatorFactory(List<DslValidator> validators) {
        this.validators = validators;
        this.validatorMap = validators.stream()
                .collect(Collectors.toMap(DslValidator::getSupportedDslType, Function.identity()));
    }
    
    /**
     * 根据DSL类型获取验证器
     * 
     * @param dslType DSL类型
     * @return 验证器，如果不支持则返回null
     */
    public DslValidator getValidator(String dslType) {
        return validators.stream()
                .filter(validator -> validator.supports(dslType))
                .findFirst()
                .orElse(null);
    }
    
    /**
     * 检查是否支持指定的DSL类型
     * 
     * @param dslType DSL类型
     * @return 是否支持
     */
    public boolean isSupported(String dslType) {
        return validators.stream()
                .anyMatch(validator -> validator.supports(dslType));
    }
    
    /**
     * 获取所有支持的DSL类型
     * 
     * @return DSL类型列表
     */
    public List<String> getSupportedDslTypes() {
        return validators.stream()
                .map(DslValidator::getSupportedDslType)
                .collect(Collectors.toList());
    }
}
